//
//  ZGTabBarViewController.m
//  扫码
//
//  Created by 鲍志刚 on 16/3/30.
//  Copyright © 2016年 鲍志刚. All rights reserved.
//

#import "ZGTabBarViewController.h"
#import "ZGNavigationViewController.h"
#import "ZGQRCodeVC.h"
#import "ZGAddAndCutVC.h"
#import "ZGStatisticsVC.h"

@interface ZGTabBarViewController ()

@end

@implementation ZGTabBarViewController

#pragma mark - 视图加载完成
- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor colorWithRed:175/255.0 green:191/255.0 blue:149/255.0 alpha:1];

    [self setUpUI];
    
}

#pragma mark - 设置子控制器
- (void)setUpUI {

//    NSArray *sbNames = @[@"ZGQRCodeVC",@"ZGAddAndCutVC",@"ZGStatisticsVC"];
//    //1 加载tabBarController的子控制器
//    [self addChildControllers:sbNames];
    
    [self loadStoryBoard:@"ZGQRCodeVC" title:@"二维码" imageName:@"tabbar_home" selectedImageName:@"tabbar_home_highlighted"];
    [self loadStoryBoard:@"ZGAddAndCutVC" title:@"增删查" imageName:@"tabbar_discover" selectedImageName:@"tabbar_discover_highlighted"];
    
    [self loadStoryBoard:@"ZGStatisticsVC" title:@"我的信息" imageName:@"tabbar_profile" selectedImageName:@"tabbar_profile_highlighted"];
    

}

#pragma mark - 通过StoryBoard加载子控制器
-(void)loadStoryBoard:(NSString *)sbName title:(NSString *)title imageName:(NSString *)imageName selectedImageName:(NSString *)selectedImageName {
    
    //1.获取SB对象
    UIStoryboard *sb = [UIStoryboard storyboardWithName:sbName bundle:nil];
    //2.获取箭头指向的控制器
    UIViewController *vc = sb.instantiateInitialViewController;
    //2.1设置控制器的title
    vc.title = title;
    //  设置tabBarItem的选中文字颜色
    NSMutableDictionary *selectedTextAttrs = [NSMutableDictionary dictionary];
    selectedTextAttrs[UITextAttributeTextColor] = [UIColor orangeColor];
    [vc.tabBarItem setTitleTextAttributes:selectedTextAttrs forState:UIControlStateSelected];
    //2.2设置图标
    vc.tabBarItem.image = [UIImage imageNamed:imageName];
    //2.3设置选中图标]
    vc.tabBarItem.selectedImage = [UIImage imageNamed:selectedImageName];
    
    //3.添加子控制器
    [self addChildViewController:vc];

}

#pragma mark - 加载tabBarController的子控制器方法1
- (void)addChildControllers:(NSArray *)sbNames {
    
    //存放子控制器的数组
    NSMutableArray *mArray = [NSMutableArray array];
    for (NSString *sbName in sbNames) {
        //1.1 加载sb中的控制器
        UIStoryboard *sb = [UIStoryboard storyboardWithName:sbName bundle:nil];
        //加载箭头指向的控制器
        ZGNavigationViewController *navC = [sb instantiateInitialViewController];
        [mArray addObject:navC];
        
        navC.title = sbName;
  
    }
    
    //当设置完子控制器之后,会立即添加对应的tabBarButton
    self.viewControllers = mArray;
    
}

#pragma mark - 接收到内存警告
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
